﻿using System;
using System.Collections.Generic;
using System.Web;
using Newtonsoft.Json;
using eCommerce.core;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;
using eCommerce.log;

namespace eCommerce.db.Action
{
    public class CategoryAction : IBaseAction
    {
        public void Do(HttpContext context)
        {
            var actionName = Utils.GetIdFromRequestString(Utils.Action, string.Empty);                                 
            
            var responseData = new ResponseData();
            switch (actionName)
            {                
                case "listtree":
                    responseData = GetListTree();
                    break;
                case "getcategory":
                    responseData = GetCategory();
                    break;

                case "save":
                    responseData = Save();
                    break;  
                case "savepriority":
                    responseData = SavePriority();
                    break;                    
                case "delete":
                    responseData = Delete();
                    break;  
    
                case "":
                    responseData.Success = false;
                    responseData.Message = "Can not found this action.";
                    break;
            }

            var jSonString = JsonConvert.SerializeObject(responseData, Utils.SerializeFormat("MM/dd/yyyy HH:mm:ss"));
            if (!string.IsNullOrEmpty(context.Request.Params["callback"]))
                jSonString = string.Format("{0}({1})", context.Request.Params["callback"], jSonString);
            context.Response.Write(jSonString);
        }
       
        private static ResponseData GetListTree()
        {
            var responseData = new ResponseData();
            var typeId = Utils.GetIdFromRequestString(Utils.TypeId, 0);

            if (typeId > 0)
            {
                try
                {
                    var lstCategory = new List<CategoryEntity>();
                    CategoryBo.GetListTree(lstCategory, typeId, 0, 0, (int)CategoryType.Active);
                    responseData.Success  = true;
                    responseData.Data     = lstCategory;
                    responseData.TotalRow = lstCategory.Count;
                }
                catch (Exception ex)
                {
                    responseData.Success = false;
                    responseData.Message = ex.Message;
                    Logger.WriteLog(Logger.LogType.Error, ex.Message);
                }
            }
           
            return responseData;
        }

        private static ResponseData GetCategory()
        {
            var responseData = new ResponseData();
            var id = Utils.GetIdFromRequestString(Utils.CategoryId, 0);

            try
            {
                var category = CategoryBo.GetById(id);
                    
                responseData.Success  = true;
                responseData.Data     = category;
                responseData.TotalRow = 1;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }

        private static ResponseData Save()
        {
            var responseData = new ResponseData();

            var id       = Utils.GetIdFromRequestString(Utils.CategoryId, 0);
            var typeId   = Utils.GetIdFromRequestString(Utils.TypeId, (int)CategoryType.Product);
            var name     = Utils.GetIdFromRequestString(Utils.Name, string.Empty);
            var parentName = Utils.GetIdFromRequestString(Utils.ParentName, string.Empty);
            var parentId = Utils.GetIdFromRequestString(Utils.ParentId, 0);
            var priority = Utils.GetIdFromRequestString(Utils.Priority, 0);
            var imgPath  = Utils.GetIdFromRequestString(Utils.ImagePath, string.Empty);
            
            try
            {
                CategoryEntity o = null;
                if (id > 0)
                {
                    o = CategoryBo.GetById(id);
                    o.CategoryId = id;
                    o.TypeId     = typeId;
                    o.Name       = name;                                        
                    o.ParentId   = parentId;
                    o.Priority   = priority;
                    o.ImagePath  = imgPath;
                    o.IsActive   = true;
                    o.Url        = typeId == (int)SettingType.Product
                                    ? string.Format("{0}/{1}", Utils.UnsignAndDash(parentName).Replace("-", ""), Utils.UnsignAndDash(name))
                                    : Utils.UnsignAndDash(name);
                }
                else
                {
                    o = new CategoryEntity();
                    o.TypeId    = typeId;
                    o.Name      = name;                    
                    o.ParentId  = parentId;
                    o.Priority  = priority;
                    o.ImagePath = imgPath;
                    o.IsActive  = true;
                    o.Url       = typeId == (int)SettingType.Product
                                    ? string.Format("{0}/{1}", Utils.UnsignAndDash(parentName).Replace("-", ""), Utils.UnsignAndDash(name))
                                    :   Utils.UnsignAndDash(name);
                }                
                
                CategoryBo.Save(o);

                responseData.Success = true;
                responseData.Data = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }

        private static ResponseData SavePriority()
        {
            var responseData = new ResponseData();

            var id       = Utils.GetIdFromRequestString(Utils.CategoryId, 0);
            var priority = Utils.GetIdFromRequestString(Utils.Priority, 0);

            try
            {
                CategoryBo.SavePriority(id, priority);

                responseData.Success = true;
                responseData.Data = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }

        private static ResponseData Delete()
        {
            var responseData = new ResponseData();

            var id = Utils.GetIdFromRequestString(Utils.CategoryId, 0);

            try
            {
                CategoryBo.Delete(id);

                responseData.Success = true;
                responseData.Data = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }
    }
}
